package com.imooc.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liu on 17-6-21.
 */
@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {

    }
    @Before("log()")
    public void doBfore(JoinPoint joinPoint) {
        ServletRequestAttributes atttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = atttributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURL());

        //method
        logger.info("method={}",request.getMethod());

        //ip
        logger.info("ip={}",request.getRemoteAddr());

        //类方法
        logger.info("class_method={}",joinPoint.getSignature().getDeclaringType() + "." + joinPoint.getSignature().getName());

        //参数(传入的)
        logger.info("args={}",joinPoint.getArgs());

    }

    @After("log()")
    public void doAfter() {
        logger.info("222222");
    }               //可以显示进程号 端口 哪个包下的哪个类打印出来的

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object) {
        logger.info("response={}",object.toString());
    }



    /*@Before("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {
        System.out.println(111111);

    }

    @After("execution(public * com.imooc.controller.GirlController.*(..))")
    public void doAfter() {
        System.out.println(222222);
     }
        */    //可以用上面的替换

}
